﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
       //控件初始化
public class CustomDateTime
{
        public CustomDateTime()
        {
            //将控件的事件句柄绑定到相应函数
            calendar.SelectionChanged += new System.EventHandler(this.calendar_SelectionChanged);
            btnSelect.Click += new System.EventHandler(this.SelDate_Click);
            
            //this.SelDate
            #region //设置控件calendar属性
            this.calendar.BackColor = this.BackColor;
            this.calendar.BorderColor = this.BorderColor;
            this.calendar.BorderStyle = this.BorderStyle;
            this.calendar.DayNameFormat = this.dayNameFormat;
            this.calendar.ForeColor = this.ForeColor;
            this.calendar.Font.Size= this.fontSize;
            this.calendar.TodayDayStyle.BackColor = this.TDSBackColor;
            this.calendar.TodayDayStyle.ForeColor = this.TDSForeColor;
            this.calendar.SelectorStyle.BackColor = this.SSBackColor;
            this.calendar.DayStyle.Font.Size = this.DSFontSize;
            this.calendar.DayStyle.ForeColor = this.DSForeColor;
            this.calendar.DayStyle.BorderColor = this.DSBorderColor;
            this.calendar.DayStyle.BorderStyle = this.DSBorderStyle;
            this.calendar.NextPrevStyle.BackColor = this.NPSBackColor;
            this.calendar.NextPrevStyle.BorderColor = this.NPSBorderColor;
            this.calendar.NextPrevStyle.BorderStyle = BorderStyle.Outset;
            this.calendar.NextPrevStyle.ForeColor = Color.Navy;
            this.calendar.NextPrevStyle.BorderWidth = base.BorderWidth;
            this.calendar.DayHeaderStyle.BackColor = ColorTranslator.FromHtml("#C0C0FF");
            this.calendar.DayHeaderStyle.ForeColor = ColorTranslator.FromHtml("ControlText");
            this.calendar.DayHeaderStyle.BorderColor = Color.Lavender;
            this.calendar.TitleStyle.BackColor = ColorTranslator.FromHtml("#C0C0FF");
            this.calendar.TitleStyle.ForeColor = Color.Navy;
            this.calendar.BorderStyle = BorderStyle.Inset;
            //this.calendar.BorderWidth = base.BorderWidth;
            this.calendar.SelectedDayStyle.BackColor =Color.MediumAquamarine;
            this.calendar.SelectedDayStyle.ForeColor = ColorTranslator.FromHtml("#C00000");
            this.calendar.WeekendDayStyle.ForeColor = Color.Red;
            this.calendar.OtherMonthDayStyle.ForeColor = Color.MidnightBlue;

            btnSelect.Text = "选择日期";
            btnSelect.Enabled = true;
            btnSelect.Visible = true;
            #endregion
            //将子控件属性与自定控件属性相关
            SelDate.Width = base.Width;
            
            calendar.Visible = false;
            calendar.TodayDayStyle.BackColor = Color.AliceBlue;
            calendar.Height = base.Height;
            calendar.Width = SelDate.Width;

            this.SelDate.ReadOnly = true;
            this.SelDate.Text = this.Value;
            this.Controls.Add(this.SelDate);
            this.Controls.Add(btnSelect);
            this.Controls.Add(this.calendar);
        }

        protected override void  Render(HtmlTextWriter writer)
        {
            this.SelDate.RenderControl(writer);
            this.btnSelect.RenderControl(writer);
            this.calendar.RenderControl(writer);

          //base.Render(writer);
        }
        protected override void RenderContents(HtmlTextWriter output)
        {
           // output.Write(Text);
        }
        //为子控件添加事件
        private void SelDate_Click(Object sender, EventArgs e)
        {
            if(this.calendar.Visible==false)
                this.calendar.Visible = true;
            this.btnSelect.Enabled = false;
        }

        protected void calendar_SelectionChanged(object sender, EventArgs e)
        {
            this.Value = calendar.SelectedDate.ToShortDateString();
            this.SelDate.Text = this.Value;
            this.btnSelect.Enabled = true;
            this.calendar.Visible = false;
        }
    }

